<script setup>
import {formatToLocalDateTime} from "@/utils/format.js";
import {wishlistAddAPI} from "@/api/FrontApi/user.js";
import {cartAddAPI} from "@/api/FrontApi/order.js";
import {gameAddToLibraryAPI} from "@/api/FrontApi/user.js";
import {useUserStore} from "@/stores/index.js";
import {useRouter} from "vue-router";
import 'element-plus/theme-chalk/el-message.css'
import {ElMessage} from "element-plus";

const props = defineProps({
  game: Object,
  gamePrice: Object
})

const router = useRouter()
const userStore = useUserStore()
const addToWishlist = async () => {
  if (userStore.token === '' || userStore.token === null) {
    // 未登录
    router.push('/login')
    return
  }
  const res = await wishlistAddAPI(props.game.id)
  if (res.code === 1) {
    ElMessage.success("添加成功")
  }
}

const addToCart = async () => {
  if (userStore.token === '' || userStore.token === null) {
    // 未登录
    router.push('/login')
    return
  }
  const res = await cartAddAPI(props.game.id)
  if (res.code === 1) {
    ElMessage.success("添加成功")
  }
}

const addToLibrary = async () => {
  if (userStore.token === '' || userStore.token === null) {
    // 未登录
    router.push('/login')
    return
  }
  const res = await gameAddToLibraryAPI(props.game.id)
  if (res.code === 1) {
    ElMessage.success("添加成功")
  }
}
</script>

<template>
  <div class="game-price-card">
    <div class="container">
      <div class="info">
        <div class="title">购买 {{ game.chineseName }}</div>
        <div class="end-date" v-if="gamePrice.discount !== 0">
          <span>特价促销</span><span>{{formatToLocalDateTime(gamePrice.discountEnd)}}</span><span>截止</span>
        </div>
      </div>
      <div class="buy">
        <div class="add-to-wishlist" @click="addToWishlist">添加至愿望单</div>
        <div class="game-price">
          <template v-if="gamePrice.currentPrice === '0.00'">
            免费开玩
          </template>
          <template v-else>
            <template v-if="gamePrice.discount === 0">
              ￥{{ gamePrice.originPrice }}
            </template>
            <template v-else>
              <div class="discount">
                <div class="discount-percent">-{{gamePrice.discount}}%</div>
                <div class="discount-price">
                  <span class="discount-origin-price">￥{{ gamePrice.originPrice }}</span>
                  <span class="discount-current-price">￥{{ gamePrice.currentPrice }}</span>
                </div>
              </div>
            </template>
          </template>
        </div>
        <div v-if="gamePrice.currentPrice === '0.00'" class="add-cart" @click="addToLibrary">获取游戏</div>
        <div v-else class="add-cart" @click="addToCart">添加至购物车</div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.game-price-card {
  .container {
    position: relative;
    .info {
      width: 820px;
      min-height: 80px;
      margin: 40px auto;
      padding: 16px;
      border-radius: 4px;
      background: linear-gradient(-60deg, rgba(226, 244, 255, 0.3) 5%, rgba(84, 107, 115, 0.3) 95%);
      .title {
        font-size: 22px;
        margin-bottom: 6px;
        color: #fff;
      }
      .end-date {
        margin-bottom: 10px;
        color: #7cb8e4;
        span:nth-child(2) {
          margin: 0 10px;
          color: $secondaryColor;
        }
      }
    }
    .buy {
      position: absolute;
      height: 40px;
      padding: 3px;
      display: flex;
      justify-content: end;
      background-color: black;
      right: 20%;
      bottom: -18%;
      border-radius: 2px;
      .add-to-wishlist {
        margin-right: 10px;
        cursor: pointer;
        height: 34px;
        padding: 0 15px;
        line-height: 34px;
        background: linear-gradient(to right, #47bfff 5%, #608dff 60%);
        border-radius: 2px;
        color: #d2efa9;
        font-size: 18px;
        text-shadow: 1px 1px 0px rgba(0, 0, 0, 0.3);
        &:hover {
          color: #fff;
          background: linear-gradient(to right, #78cfff 5%, #41bdff 60%);
        }
      }
      .game-price {
        margin: 0 4px;
        height: 34px;
        line-height: 34px;
        font-size: 16px;
        color: #c6d4df;
        .discount {
          height: 34px;
          display: flex;
          .discount-percent {
            line-height: 34px;
            padding: 0 4px;
            background-color: $discountBackgroundColor;
            color: $discountTextColor;
            font-size: 24px;
            text-shadow: 1px 1px 4px rgba(0, 0, 0, 1);
          }
          .discount-price {
            height: 34px;
            display: flex;
            flex-direction: column;
            text-align: center;
            background-color: #525252;
            .discount-origin-price {
              line-height: 14px;
              font-size: 12px;
              margin: 0 20px;
              position: relative;
              color: #738895;
              &:before {
                content: '';
                left: 0;
                right: 0;
                position: absolute;
                top: 43%;
                border-bottom: 1.5px solid #738895;
                transform: skewY(-8deg);
                box-shadow: 0 0 2px black;
              }
            }
            .discount-current-price {
              height: 20px;
              line-height: 20px;
              font-size: 18px;
              padding: 0 4px 0 0;
            }
          }
        }

      }
      .add-cart {
        cursor: pointer;
        height: 34px;
        padding: 0 15px;
        line-height: 34px;
        background: linear-gradient(to right, #75b022 5%, #588a1b 95%);
        border-radius: 2px;
        color: #d2efa9;
        font-size: 18px;
        text-shadow: 1px 1px 0px rgba(0, 0, 0, 0.3);

        &:hover {
          color: #fff;
          background: linear-gradient(to right, #96e12d 5%, #75b022 95%);
        }
      }
    }
  }
}
</style>
